package cool.taomu.toolkit.service.kademlia.service

import java.util.Map
import cool.taomu.toolkit.service.kademlia.entity.KPeerNode
import com.google.common.util.concurrent.RateLimiter
import java.util.concurrent.ConcurrentHashMap

class PeerRateLimiter {
    val Map<KPeerNode, RateLimiter> limiters = new ConcurrentHashMap();
    val double permitsPerSecond;

    new(double permitsPerSecond) {
        this.permitsPerSecond = permitsPerSecond;
    }

    def RateLimiter getLimiter(KPeerNode node) {
        return limiters.computeIfAbsent(node, [RateLimiter.create(permitsPerSecond)]);
    }
}
